home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / beos / PPBeDevKit.ZIP / PLAYERPR.TAR / PlayerPRO / Source / Import-Export / DMF.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-26  |  21.7 KB  |  796 lines

  1. /********************                        ***********************/
  2. //
  3. //    Player PRO 5.0 - DRIVER SOURCE CODE -
  4. //
  5. //    Library Version 5.0
  6. //
  7. //    To use with MAD Library for Mac: Symantec, CodeWarrior and MPW
  8. //
  9. //    Antoine ROSSET
  10. //    16 Tranchees
  11. //    1206 GENEVA
  12. //    SWITZERLAND
  13. //
  14. //    COPYRIGHT ANTOINE ROSSET 1996, 1997, 1998
  15. //
  16. //    Thank you for your interest in PlayerPRO !
  17. //
  18. //    FAX:                (+41 22) 346 11 97
  19. //    PHONE:             (+41 79) 203 74 62
  20. //    Internet:     RossetAntoine@bluewin.ch
  21. //
  22. /********************                        ***********************/
  23.  
  24. #include "IT.h"
  25. #include "MAD.h"
  26. #include "RDriver.h"
  27.  
  28. #if defined(powerc) || defined(__powerc)
  29. enum {
  30.         PlayerPROPlug = kCStackBased
  31.         | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
  32.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( OSType)))
  33.         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( Ptr)))
  34.         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( MADMusic*)))
  35.         | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( PPInfoRec*)))
  36.         | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof( MADDriverSettings*)))
  37. };
  38.  
  39. ProcInfoType __procinfo = PlayerPROPlug;
  40. #else
  41. #include <A4Stuff.h>
  42. #endif
  43.  
  44. #define LOW(para) ((para) & 15)
  45. #define HI(para) ((para) >> 4)
  46.  
  47. unsigned long Tdecode32( void *msg_buf)
  48. {
  49.   unsigned char *buf = msg_buf;
  50.   
  51.   return( (unsigned long) buf[3] << 24) | ( (unsigned long) buf[2] << 16) | ( (unsigned long) buf[ 1] << 8) | ( (unsigned long) buf[0]);
  52. }
  53.  
  54. short Tdecode16( void *msg_buf)
  55. {
  56.   unsigned char *buf = msg_buf;
  57.   
  58.   return ( (short) buf[1] << 8) | ( (short) buf[0]);
  59. }
  60. /*
  61. Cmd* GetMADCommand( register short PosX, register short    TrackIdX, register PatData*    tempMusicPat)
  62. {
  63.     if( PosX < 0) PosX = 0;
  64.     else if( PosX >= tempMusicPat->header.size) PosX = tempMusicPat->header.size -1;
  65.         
  66.     return( & (tempMusicPat->Cmds[ (tempMusicPat->header.size * TrackIdX) + PosX]));
  67. }
  68.  
  69. Ptr MADPlugNewPtr( long size, MADDriverSettings* init)
  70. {
  71.     if( init->sysMemory) return NewPtrSys( size);
  72.     else return NewPtr( size);
  73. }
  74.  
  75. Ptr MADPlugNewPtrClear( long size, MADDriverSettings* init)
  76. {
  77.     if( init->sysMemory) return NewPtrSysClear( size);
  78.     else return NewPtrClear( size);
  79. }
  80.  
  81. void pStrcpy(register unsigned char *s1, register unsigned char *s2)
  82. {
  83.     register short len, i;
  84.     
  85.     len = *s2;
  86.     for ( i = 0; i <= len; i++) s1[ i] = s2[ i];
  87. }
  88.  
  89. void strncpy( Ptr dst, Ptr str, long size)
  90. {
  91.     BlockMove( str, dst, size);
  92. }
  93.  
  94. */
  95. void mystrcpy( Ptr a, Ptr b)
  96. {
  97.     BlockMove( b + 1, a, b[ 0]);
  98. }
  99.  
  100.  
  101. void ConvertITEffect( Byte B0, Byte B1, Byte *Cmd, Byte *Arg)
  102. {
  103.     Str255    tStr;
  104.     Byte        LoB1 = LOW( B1);
  105.     Byte        HiB1 = HI( B1);
  106.     
  107.     switch( B0 + 0x40)
  108.     {
  109.         default:    *Cmd = 0;            *Arg = 0;        break;
  110.         // Speed
  111.         case 'A':    *Cmd = speedE;        *Arg = B1;    break;
  112.         // Tempo
  113.         case 'T':    *Cmd = speedE;        *Arg = B1;    break;
  114.  
  115.         case 'B':    *Cmd = fastskipE;        *Arg = B1;    break;
  116.  
  117.         case 'C':    *Cmd = skipE;            *Arg = B1;    break;
  118.  
  119.         case 'D':
  120.             if( LoB1 == 0 || HiB1 == 0)        // Slide volume
  121.             {
  122.                 *Cmd = slidevolE;        *Arg = B1;
  123.             }
  124.             else if( HiB1 == 0x0F)        // Fine Slide volume DOWN
  125.             {
  126.                 *Cmd = extendedE;
  127.                 *Arg = 11 << 4;
  128.                 *Arg += LoB1;
  129.             }
  130.             else if( LoB1 == 0x0F)        // Fine Slide volume UP
  131.             {
  132.                 *Cmd = extendedE;
  133.                 *Arg = 10 << 4;
  134.                 *Arg += HiB1;
  135.             }
  136.         break;
  137.         
  138.         case 'E':
  139.             if( HiB1 == 0x0F)        // FineSlide DOWN
  140.             {
  141.                 *Cmd = extendedE;
  142.                 *Arg = 2 << 4;        //not supported
  143.                 *Arg += LoB1;
  144.             }
  145.             else if( HiB1 == 0x0E)    // ExtraFineSlide DOWN
  146.             {
  147.                 *Cmd = 0;            *Arg = 0;        //not supported
  148.             }
  149.             else                    // Slide DOWN
  150.             {
  151.                 *Cmd = upslideE;        *Arg = B1;
  152.             }
  153.         break;
  154.  
  155.         case 'F':
  156.             if( HiB1 == 0x0F)        // FineSlide UP
  157.             {
  158.                 *Cmd = extendedE;
  159.                 *Arg = 1 << 4;        //not supported
  160.                 *Arg += LoB1;
  161.             }
  162.             else if( HiB1 == 0x0E)    // ExtraFineSlide UP
  163.             {
  164.                 *Cmd = 0;            *Arg = 0;        //not supported
  165.             }
  166.             else                    // Slide UP
  167.             {
  168.                 *Cmd = downslideE;        *Arg = B1;
  169.             }
  170.         break;
  171.  
  172.         case 'G':    *Cmd = portamentoE;    *Arg = B1;    break;
  173.         case 'H':    *Cmd = vibratoE;        *Arg = B1;    break;
  174.         
  175.         case 'J':    *Cmd = arpeggioE;        *Arg = B1;    break;
  176.         case 'K':    *Cmd = vibratoslideE;    *Arg = B1;    break;
  177.         case 'L':    *Cmd = portaslideE;        *Arg = B1;    break;
  178.         case 'O':    *Cmd = offsetE;        *Arg = B1;    break;
  179.  
  180.         case 'S':        // Special Effects
  181.             switch( HiB1)
  182.             {
  183.                 default:    *Cmd = 0;        *Arg = 0;                            break;
  184.                 case 2:    *Cmd = extendedE;    *Arg = 5 << 4;        *Arg += LoB1;        break;    // FineTune
  185.                 case 3:    *Cmd = extendedE;    *Arg = 4 << 4;        *Arg += LoB1;        break;    // Set Vibrato WaveForm
  186.                 case 4:    *Cmd = extendedE;    *Arg = 7 << 4;        *Arg += LoB1;        break;    // Set Tremolo WaveForm
  187.                 case 0xB:    *Cmd = extendedE;    *Arg = 6 << 4;        *Arg += LoB1;        break;    // Loop pattern
  188.                 case 0xC:    *Cmd = extendedE;    *Arg = 12 << 4;    *Arg += LoB1;        break;    // Cut sample
  189.                 case 0xD:    *Cmd = extendedE;    *Arg = 13 << 4;    *Arg += LoB1;        break;    // Delay sample
  190.                 case 0xE:    *Cmd = extendedE;    *Arg = 14 << 4;    *Arg += LoB1;        break;    // Delay pattern
  191.             }
  192.         break;
  193.     }
  194. }
  195.  
  196. void ConvertMADEffect( Byte Cmd, Byte Arg, Byte *B0, Byte *B1)
  197. {
  198.     *B0 = 0;
  199.     *B1 = 0;
  200.  
  201.     switch( Cmd)
  202.     {
  203.         case speedE:        *B0 = 'A' - 0x40;    *B1 = Arg;    break;
  204.         case fastskipE:        *B0 = 'B' - 0x40;    *B1 = Arg;    break;
  205.         case skipE:        *B0 = 'C' - 0x40;    *B1 = Arg;    break;
  206.         case slidevolE:        *B0 = 'D' - 0x40;    *B1 = Arg;    break;
  207.         case upslideE:        *B0 = 'E' - 0x40;    *B1 = Arg;    break;
  208.         case downslideE:    *B0 = 'F' - 0x40;    *B1 = Arg;    break;
  209.         case portamentoE:    *B0 = 'G' - 0x40;    *B1 = Arg;    break;
  210.         case vibratoE:        *B0 = 'H' - 0x40;    *B1 = Arg;    break;
  211.         
  212.         case arpeggioE:        *B0 = 'J' - 0x40;    *B1 = Arg;    break;
  213.         case vibratoslideE:    *B0 = 'K' - 0x40;    *B1 = Arg;    break;
  214.         case portaslideE:    *B0 = 'L' - 0x40;    *B1 = Arg;    break;
  215.         case offsetE:        *B0 = 'O' - 0x40;    *B1 = Arg;    break;
  216.         
  217.         case extendedE:
  218.             switch( (Arg & 0xF0) >> 4)
  219.             {
  220.                 case 1:    *B0 = 'F' - 0x40;    *B1 = 0xf0 + (Arg & 0x0F);        break;
  221.                 case 2:    *B0 = 'E' - 0x40;    *B1 = 0xf0 + (Arg & 0x0F);        break;
  222.                 
  223.                 case 5:    *B0 = 'S' - 0x40;    *B1 = 0x20 + (Arg & 0x0F);        break;
  224.                 
  225.                 case 4:    *B0 = 'S' - 0x40;    *B1 = 0x30 + (Arg & 0x0F);        break;
  226.                 case 7:    *B0 = 'S' - 0x40;    *B1 = 0x40 + (Arg & 0x0F);        break;
  227.                 case 6:    *B0 = 'S' - 0x40;    *B1 = 0xb0 + (Arg & 0x0F);        break;
  228.                 case 12:    *B0 = 'S' - 0x40;    *B1 = 0xc0 + (Arg & 0x0F);        break;
  229.                 case 13:    *B0 = 'S' - 0x40;    *B1 = 0xd0 + (Arg & 0x0F);        break;
  230.                 case 14:    *B0 = 'S' - 0x40;    *B1 = 0xe0 + (Arg & 0x0F);        break;
  231.                 
  232.                 case 11:    *B0 = 'D' - 0x40;    *B1 = 0xf0 + (Arg & 0x0F);        break;
  233.                 case 10:    *B0 = 'D' - 0x40;    *B1 = 0x0f + ((Arg & 0x0F)<<4);    break;
  234.             }
  235.         break;
  236.     }
  237. }
  238.  
  239. OSErr ConvertIT2Mad( Ptr theIT, long MODSize, MADMusic *theMAD, MADDriverSettings *init)
  240. {
  241.     long                 i, PatMax, x, z, channel, Row;
  242.     long                 sndSize, OffSetToSample, OldTicks, starting;
  243.     Ptr                    MaxPtr;
  244.     OSErr                theErr;
  245.     Ptr                    theInstrument[ 64], destPtr;
  246.     Byte                tempChar, *theITCopy;
  247.     short                Note, Octave, maxTrack;
  248.     short                ITperiod[ 12] = {1712,1616,1524,1440,1356,1280,1208,1140,1076,1016, 960, 907};
  249.     long                note_st3period;
  250.     long                note_amigaperiod;
  251.     Byte                *ChannelSettings;
  252.     
  253.     /**** Variables pour le MAD ****/
  254.     Cmd                *aCmd;
  255.  
  256.     /**** Variables pour le IT ****/
  257.     
  258.     ITForm            ITinfo;
  259.     /********************************/
  260.  
  261.     for( i = 0 ; i < 64; i ++)
  262.     {
  263.         theInstrument[ i] = 0L;
  264.     }
  265.  
  266.     /**** Header principal *****/
  267.     theITCopy = (Byte*) theIT;
  268.     
  269.     BlockMove( theITCopy, &ITinfo, sizeof( ITinfo));
  270.     theITCopy += 192;
  271.     
  272.     ITinfo.orderNum    = Tdecode16(  &ITinfo.orderNum);
  273.     ITinfo.insNum        = Tdecode16(  &ITinfo.insNum);
  274.     ITinfo.smpNum        = Tdecode16(  &ITinfo.smpNum);
  275.     ITinfo.patNum        = Tdecode16(  &ITinfo.patNum);
  276.     ITinfo.flags        = Tdecode16(  &ITinfo.flags);
  277.     ITinfo.cwtv            = Tdecode16(  &ITinfo.cwtv);
  278.     
  279.     /**** Order Num *****/
  280.     ITinfo.orders = (unsigned char *) MADPlugNewPtr( ITinfo.orderNum, init);
  281.     if( ITinfo.orders == 0L) return MADNeedMemory;
  282.     BlockMove( theITCopy, ITinfo.orders, ITinfo.orderNum);
  283.     theITCopy += ITinfo.orderNum;
  284.     
  285.     /**** Ins Num *****/
  286.     ITinfo.parapins = (long *) MADPlugNewPtr( ITinfo.insNum * 4L, init);
  287.     if( ITinfo.parapins == 0L) return MADNeedMemory;
  288.     BlockMove( theITCopy, ITinfo.parapins, ITinfo.insNum * 4L);
  289.     theITCopy += ITinfo.insNum * 4L;
  290.     for( i = 0; i < ITinfo.insNum; i++)
  291.     {
  292.         ITinfo.parapins[ i] = Tdecode32(  &ITinfo.parapins[ i]);
  293.     }
  294.     
  295.     /**** Samp Num *****/
  296.     ITinfo.parapsamp = (long *) MADPlugNewPtr( ITinfo.smpNum * 4L, init);
  297.     if( ITinfo.parapsamp == 0L) return MADNeedMemory;
  298.     BlockMove( theITCopy, ITinfo.parapsamp, ITinfo.smpNum * 4L);
  299.     theITCopy += ITinfo.smpNum * 4L;
  300.     for( i = 0; i < ITinfo.smpNum; i++)
  301.     {
  302.         ITinfo.parapsamp[ i] = Tdecode32(  &ITinfo.parapsamp[ i]);
  303.     }
  304.     
  305.     /**** Pat Num *****/
  306.     ITinfo.parappat = (long *) MADPlugNewPtr( ITinfo.patNum * 4L, init);
  307.     if( ITinfo.parappat == 0L) return MADNeedMemory;
  308.     BlockMove( theITCopy, ITinfo.parappat, ITinfo.patNum * 4L);
  309.     theITCopy += ITinfo.patNum * 4L;
  310.     for( i = 0; i < ITinfo.patNum; i++)
  311.     {
  312.         ITinfo.parappat[ i] = Tdecode32(  &ITinfo.parappat[ i]);
  313.     }
  314.     
  315.     /**** Ins Data ****/
  316.     if( ITinfo.insNum > 64) ITinfo.insNum = 64;
  317.     ITinfo.insdata = (ITInsForm *) MADPlugNewPtr( sizeof(ITInsForm) * ITinfo.insNum, init);
  318.     if( ITinfo.insdata == 0L) return MADNeedMemory;
  319.     for (i = 0; i < ITinfo.insNum; i++)
  320.     {
  321.         theITCopy = (Byte*) theIT;
  322.         theITCopy += ITinfo.parapins[i];
  323.         
  324.         BlockMove( theITCopy, &ITinfo.insdata[i], sizeof( ITInsForm));
  325.         
  326.         if( ITinfo.insdata[i].ID != 'IMPI') Debugger();
  327.         
  328. /*    if( ITinfo.insdata[i].insflags&1)
  329.         {
  330.             ITinfo.insdata[i].insloopbeg        = Tdecode32( &ITinfo.insdata[i].insloopbeg);
  331.             ITinfo.insdata[i].insloopend        = Tdecode32( &ITinfo.insdata[i].insloopend);
  332.         }
  333.         else
  334.         {
  335.             ITinfo.insdata[i].insloopbeg        = 0;
  336.             ITinfo.insdata[i].insloopend        = 0;
  337.         }
  338.         
  339.         ITinfo.insdata[i].c2spd                        = Tdecode32(  &ITinfo.insdata[i].c2spd);
  340.         ITinfo.insdata[i].insgvspos                = Tdecode16(  &ITinfo.insdata[i].insgvspos);
  341.         ITinfo.insdata[i].insint512                = Tdecode16(  &ITinfo.insdata[i].insint512);
  342.         ITinfo.insdata[i].insintlastused    = Tdecode32( &ITinfo.insdata[i].insintlastused);
  343.         
  344.         if (ITinfo.insdata[i].instype == 1 && ITinfo.insdata[i].inspack == 0 && ITinfo.insdata[i].inssig == 'SCRS')
  345.     {
  346.         long tempL;
  347.  
  348.         theITCopy = (Byte*) theIT;
  349.         
  350.             tempL = (((long)ITinfo.insdata[i].memsegh)<<16|ITinfo.insdata[i].memsegl)<<4;
  351.         
  352.         theITCopy += tempL;
  353.         
  354.         theInstrument[ i] = (Ptr) theITCopy;
  355.     }
  356.     else
  357.     {
  358.         theInstrument[ i] = 0L;
  359.     }*/
  360.     }
  361.     
  362.     /**** Samp Data ****/
  363.     //if( ITinfo.insNum > 64) ITinfo.insNum = 64;
  364.     ITinfo.sampdata = (ITSampForm *) MADPlugNewPtr( sizeof(ITSampForm) * ITinfo.smpNum, init);
  365.     if( ITinfo.sampdata == 0L) return MADNeedMemory;
  366.     for (i = 0; i < ITinfo.smpNum; i++)
  367.     {
  368.         theITCopy = (Byte*) theIT;
  369.         theITCopy += ITinfo.parapsamp[i];
  370.         
  371.         BlockMove( theITCopy, &ITinfo.sampdata[i], sizeof( ITSampForm));
  372.         
  373.         if( ITinfo.sampdata[i].ID != 'IMPS') Debugger();
  374.         
  375.         ITinfo.sampdata[i].length                    = Tdecode32( &ITinfo.sampdata[i].length);
  376.         ITinfo.sampdata[i].loopBegin            = Tdecode32( &ITinfo.sampdata[i].loopBegin);
  377.         ITinfo.sampdata[i].loopEnd                = Tdecode32( &ITinfo.sampdata[i].loopEnd);
  378.         ITinfo.sampdata[i].C5Speed                = Tdecode32( &ITinfo.sampdata[i].C5Speed);
  379.         ITinfo.sampdata[i].SusLoopBegin        = Tdecode32( &ITinfo.sampdata[i].SusLoopBegin);
  380.         ITinfo.sampdata[i].SusLoopEnd            = Tdecode32( &ITinfo.sampdata[i].SusLoopEnd);
  381.         ITinfo.sampdata[i].samplePtr            = Tdecode32( &ITinfo.sampdata[i].samplePtr);
  382.         
  383.         theInstrument[ i] = (Ptr) theIT;
  384.         theInstrument[ i] += ITinfo.sampdata[i].samplePtr;
  385.     }
  386.     
  387.     
  388.     // ******** Le IT a ÄtÄ lu et analysÄ ***********
  389.     // ******** Copie des informations dans le MAD ***
  390.     
  391.     theMAD->header = (MADSpec*) MADPlugNewPtrClear( sizeof( MADSpec), init);
  392.     if( theMAD->header == 0L) return MADNeedMemory;
  393.         
  394.     theMAD->header->MAD = 'MADI';
  395.     for(i=0; i<32; i++) theMAD->header->name[i] = 0;
  396.     for(i=0; i<28; i++) theMAD->header->name[i] = ITinfo.name[i];
  397.     
  398.     mystrcpy( theMAD->header->infos, (Ptr) "\pConverted by PlayerPRO IT Plug (⌐Antoine ROSSET <rossetantoine@bluewin.ch>)");
  399.     
  400.     theMAD->header->numPat            = ITinfo.patNum;
  401.     theMAD->header->numPointers    = ITinfo.orderNum;
  402.     theMAD->header->speed                = ITinfo.iSpeed;
  403.     theMAD->header->tempo                = ITinfo.iTempo;
  404.     
  405.     for(i=0; i<128; i++) theMAD->header->oPointers[ i] = 0;
  406.     for(i=0; i<ITinfo.orderNum; i++)
  407.     {
  408.         theMAD->header->oPointers[ i] = ITinfo.orders[i];
  409.         
  410.         if( theMAD->header->oPointers[ i] < 0 || theMAD->header->oPointers[ i] >= ITinfo.patNum) theMAD->header->oPointers[ i] = 0;
  411.     }
  412.  
  413. for( i = 0; i < MAXTRACK; i++)
  414. {
  415.     if( i % 2 == 0) theMAD->header->chanPan[ i] = MAX_PANNING/4;
  416.     else theMAD->header->chanPan[ i] = MAX_PANNING - MAX_PANNING/4;
  417.     
  418.     theMAD->header->chanVol[ i] = MAX_VOLUME;
  419. }
  420.  
  421.     theMAD->header->generalVol        = 64;
  422.     theMAD->header->generalSpeed    = 80;
  423.     theMAD->header->generalPitch    = 80;
  424.  
  425.     // ********************
  426.     // ***** INSTRUMENTS *****
  427.     // ********************
  428.  
  429.     theMAD->fid = ( InstrData*) MADPlugNewPtrClear( sizeof( InstrData) * (long) MAXINSTRU, init);
  430.     if( !theMAD->fid) return MADNeedMemory;
  431.     
  432.     theMAD->sample = ( sData**) MADPlugNewPtrClear( sizeof( sData*) * (long) MAXINSTRU * (long) MAXSAMPLE, init);
  433.     if( !theMAD->sample) return MADNeedMemory;
  434.     
  435.     for( i = 0; i < MAXINSTRU; i++) theMAD->fid[ i].firstSample = i * MAXSAMPLE;
  436.  
  437.     for(i  = 0 ; i < MAXINSTRU; i++)
  438.     {
  439.         for( x = 0; x < MAXSAMPLE; x++) theMAD->sample[ i*MAXSAMPLE + x] = 0L;
  440.         
  441.         theMAD->fid[i].numSamples    = 0;
  442.     }
  443.     
  444.  
  445.     
  446.     if( ITinfo.flags & 2)        // USE INSTRUMENTS
  447.     {
  448.         
  449.     }
  450.     else                                        // USE SAMPLES AS INSTRUMENTS
  451.     {
  452.         for(i=0; i<ITinfo.smpNum; i++)
  453.         {
  454.             InstrData        *curIns = &theMAD->fid[ i];
  455.             
  456.             curIns->type    = 0;
  457.             
  458.             if( theInstrument[ i] != 0L)
  459.             {
  460.                 sData    *curData;
  461.                 
  462.                 curIns->numSamples    = 1;
  463.                 curIns->volFade            = DEFAULT_VOLFADE;
  464.                 
  465.                 curData = theMAD->sample[ i*MAXSAMPLE + 0] = (sData*) MADPlugNewPtrClear( sizeof( sData), init);
  466.                 if( curData == 0L) return MADNeedMemory;
  467.                 
  468.                 curData->size            = ITinfo.sampdata[i].length;
  469.                 curData->loopBeg     = ITinfo.sampdata[i].loopBegin;
  470.                 curData->loopSize    = ITinfo.sampdata[i].loopEnd - ITinfo.sampdata[i].loopBegin;
  471.                 curData->vol            = ITinfo.sampdata[i].GvL;
  472.                 curData->c2spd        = ITinfo.sampdata[i].C5Speed;
  473.                 curData->loopType    = 0;
  474.                 curData->amp            = 8;        if( ITinfo.sampdata[i].Flag&1)    curData->amp        = 16;
  475.                 
  476.                 curData->relNote    = 0;
  477.                 for( x = 0; x < 28; x++) theMAD->fid[i].name[x] = ITinfo.sampdata[i].DOSName[x];
  478.                 
  479.                 curData->data         = MADPlugNewPtr( curData->size, init);
  480.                 if( curData->data == 0L) return MADNeedMemory;
  481.                 
  482.                 if( curData->data == 0L) DebugStr("\pInstruments: I NEED MEMORY !!! NOW !");
  483.                 
  484.                 if( curData->data != 0L)
  485.                 {
  486.                     BlockMove( theInstrument[i], curData->data, curData->size);
  487.                     
  488.                     /*if( ITinfo.ffv != 1)
  489.                     {
  490.                         long temp;
  491.                         
  492.                         for( temp = 0; temp < curData->size; temp++) *(curData->data + temp) -= 0x80;
  493.                     }*/
  494.                 }
  495.             }
  496.             else curIns->numSamples = 0;
  497.         }
  498.     }
  499.     //    *********************
  500.     //    *           Check MaxTrack         *
  501.     //    *********************
  502.     /*
  503.     maxTrack = 0;
  504.     for(i=0;i<32;i++)
  505.     {
  506.         if(ITinfo.chanset[ i]<16) maxTrack++;
  507.     }
  508.     maxTrack++;
  509.     maxTrack /= 2;
  510.     maxTrack *= 2;
  511.     */
  512.     // ********************
  513.     // ***** TEMPORAIRE ******
  514.     // ********************
  515.     
  516.     theMAD->header->numChn = 32;
  517.     
  518.     starting = 0;
  519.  
  520.     for( i = 0; i < MAXPATTERN; i++) theMAD->partition[ i] = 0L;
  521.     for( i = 0; i < theMAD->header->numPat ; i++)
  522.     {
  523.         ITPatForm        *curITPat;
  524.         
  525.         curITPat = (ITPatForm*) (theIT + ITinfo.parappat[i]);
  526.         
  527.         curITPat->length     = Tdecode16(  &curITPat->length);
  528.         curITPat->row         = Tdecode16(  &curITPat->row);
  529.         
  530.         theMAD->partition[ i] = (PatData*) MADPlugNewPtrClear( sizeof( PatHeader) + theMAD->header->numChn * curITPat->row * sizeof( Cmd), init);
  531.         if( theMAD->partition[ i] == 0L) return MADNeedMemory;
  532.         
  533.         theMAD->partition[ i]->header.size             = curITPat->row;
  534.         theMAD->partition[ i]->header.compMode     = 'NONE';
  535.         
  536.         for( x = 0; x < 20; x++) theMAD->partition[ i]->header.name[ x] = 0;
  537.         
  538.         MaxPtr = (Ptr) theMAD->partition[ i];
  539.         MaxPtr += sizeof( PatHeader) + theMAD->header->numChn * curITPat->row * sizeof( Cmd);
  540.         
  541.         for( Row = 0; Row < curITPat->row; Row++)
  542.         {
  543.             for(z = 0; z < theMAD->header->numChn; z++)
  544.             {
  545.                 aCmd = GetMADCommand( Row, z, theMAD->partition[ i]);
  546.                 
  547.                 aCmd->note        = 0xFF;
  548.                 aCmd->ins            = 0;
  549.                 aCmd->cmd        = 0;
  550.                 aCmd->arg        = 0;
  551.                 aCmd->vol        = 0xFF;
  552.             }
  553.         }
  554.         
  555.         if( ITinfo.parappat[i] > 0)
  556.         {
  557.             Ptr             curDataPat = curITPat->data;
  558.             Byte            maskvariable = 0;
  559.             Boolean        NeedChannelToRead = true;
  560.             
  561.             Row = 0;
  562.             while( Row < curITPat->row)
  563.             {
  564.                     tempChar = *curDataPat;
  565.                     curDataPat++;
  566.     
  567.                     if( tempChar == 0) Row++;
  568.                     else
  569.                     {
  570.                         if( NeedChannelToRead)
  571.                         {
  572.                             // Channel
  573.                             channel = (tempChar-1) & 63;
  574.                             if( channel >= 0 && channel < theMAD->header->numChn) aCmd = GetMADCommand( Row, channel, theMAD->partition[ i]);
  575.                             else aCmd = 0L;
  576.                         }
  577.                         
  578.                         if(tempChar & 128)
  579.                         {
  580.                             maskvariable = *curDataPat;
  581.                             curDataPat++;
  582.                         }
  583.                         
  584.                         // NOTE
  585.                         if( maskvariable & 1 || maskvariable & 16)
  586.                         {
  587.                             if( aCmd != 0L)
  588.                             {
  589.                                 aCmd->note = *curDataPat;
  590.                                 if( aCmd->note == 255) aCmd->note = 0xFE;
  591.                                 else if( aCmd->note < 0 || aCmd->note >= NUMBER_NOTES) aCmd->note = 0xFF;
  592.                             }
  593.                             curDataPat ++;
  594.                         }
  595.                         
  596.                         // INSTRUMENT
  597.                         if( maskvariable & 2 || maskvariable & 32)
  598.                         {
  599.                             if( aCmd != 0L)
  600.                             {
  601.                                 aCmd->ins = 1 + *curDataPat;
  602.                             }
  603.                             curDataPat++;
  604.                         }
  605.                         
  606.                         // VOLUME
  607.                         if( maskvariable & 4 || maskvariable & 64)
  608.                         {
  609.                             if( aCmd != 0L)
  610.                             {
  611.                                 aCmd->vol = *curDataPat;
  612.                                 if( aCmd->vol > 64) aCmd->vol = 64;
  613.                                 aCmd->vol += 0x10;
  614.                             }
  615.                             curDataPat++;
  616.                         }
  617.                         else aCmd->vol = 255;
  618.                         
  619.                         // PARAMETER
  620.                         if( tempChar & 8 || maskvariable & 128)
  621.                         {
  622.                             if( aCmd != 0L)
  623.                             {
  624.                             /*    if( theITCopy[ 0] != 255)
  625.                                 {
  626.                                     ConvertITEffect( theITCopy[ 0], theITCopy[ 1], &aCmd->cmd, &aCmd->arg);
  627.                                 }    */
  628.                             }
  629.                             curDataPat += 2;
  630.                         }
  631.                     }
  632.             }
  633.         }
  634.     }
  635.     
  636.     DisposePtr( (Ptr) ITinfo.orders);            DisposePtr( (Ptr) ITinfo.parapins);
  637.     DisposePtr( (Ptr) ITinfo.parappat);        DisposePtr( (Ptr) ITinfo.insdata);
  638.     DisposePtr( (Ptr) ITinfo.parapsamp);
  639.  
  640.     return noErr;
  641. }
  642.  
  643. OSErr ExtractITInfo( PPInfoRec *info, Ptr AlienFile)
  644. {
  645.     short        i, maxInstru, tracksNo;
  646.     ITForm        ITinfo;
  647.     /********************************/
  648.  
  649.     /**** Header principal *****/
  650.     BlockMove( AlienFile, &ITinfo, 96);
  651.     
  652.     /*** Signature ***/
  653.     
  654.     info->signature = 'IT  ';
  655.     
  656.     /*** Internal name ***/
  657.     
  658.     ITinfo.name[ 26] = '\0';
  659.     pStrcpy( info->internalFileName, CtoPstr( ITinfo.name));
  660.     
  661.     /*** Total Patterns ***/
  662.     
  663.     info->totalPatterns = Tdecode16(  &ITinfo.patNum);
  664.     
  665.     /*** Partition Length ***/
  666.     
  667.     info->partitionLength = Tdecode16( &ITinfo.orderNum);
  668.     
  669.     /*** Total Instruments ***/
  670.     
  671.     info->totalInstruments = Tdecode16(  &ITinfo.insNum);
  672.     
  673.     /*** Tracks ***/
  674.     
  675.     //info->totalTracks     = Tdecode16(  &ITinfo.insNum);
  676.     
  677.     pStrcpy( info->formatDescription, "\pIT Plug");
  678.  
  679.     return noErr;
  680. }
  681.  
  682. OSErr TestITFile( Ptr AlienFile)
  683. {
  684. ITForm    *myIT = ( ITForm*) AlienFile;
  685.  
  686.     if( myIT->ID == 'IMPM') return   noErr;
  687.     else return  MADFileNotSupportedByThisPlug;
  688. }
  689.  
  690. OSErr TEST2main( OSType order, char *AlienFileFSSpec, MADMusic *MadFile, PPInfoRec *info, MADDriverSettings *init)
  691. {
  692.     OSErr    myErr;
  693.     Ptr        AlienFile;
  694.     short    vRefNum, iFileRefI;
  695.     long        dirID, sndSize;
  696.     
  697. #ifndef powerc
  698.     long    oldA4 = SetCurrentA4();             //this call is necessary for strings in 68k code resources
  699. #endif
  700.  
  701.     HGetVol( 0L, &vRefNum, &dirID);
  702.     HSetVol( 0L, AlienFileFSSpec->vRefNum, AlienFileFSSpec->parID);
  703.  
  704.     myErr = noErr;
  705.  
  706.     switch( order)
  707.     {
  708.         case 'IMPL':
  709.             myErr = FSOpen( AlienFileFSSpec->name, 0, &iFileRefI);
  710.             if( myErr == noErr)
  711.             {
  712.                 GetEOF( iFileRefI, &sndSize);
  713.             
  714.                 // ** MEMORY Test Start
  715.                 AlienFile = MADPlugNewPtr( sndSize * 2L, init);
  716.                 if( AlienFile == 0L) myErr = MADNeedMemory;
  717.                 // ** MEMORY Test End
  718.                 
  719.                 else
  720.                 {
  721.                     DisposePtr( AlienFile);
  722.                     
  723.                     AlienFile = MADPlugNewPtr( sndSize, init);
  724.                     if( AlienFile == 0L) myErr = MADNeedMemory;
  725.                     else
  726.                     {
  727.                         myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  728.                         if( myErr == noErr)
  729.                         {
  730.                             myErr = TestITFile( AlienFile);
  731.                             if( myErr == noErr)
  732.                             {
  733.                                 myErr = ConvertIT2Mad( AlienFile,  GetPtrSize( AlienFile), MadFile, init);
  734.                             }
  735.                         }
  736.                     }
  737.                     DisposePtr( AlienFile);    AlienFile = 0L;
  738.                 }
  739.                 FSClose( iFileRefI);
  740.             }
  741.         break;
  742.         
  743.         case 'TEST':
  744.             myErr = FSOpen( AlienFileFSSpec->name, 0, &iFileRefI);
  745.             if( myErr == noErr)
  746.             {
  747.                 sndSize = 1024L;
  748.                 
  749.                 AlienFile = MADPlugNewPtr( sndSize, init);
  750.                 if( AlienFile == 0L) myErr = MADNeedMemory;
  751.                 else
  752.                 {
  753.                     myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  754.                     myErr = TestITFile( AlienFile);
  755.                     
  756.                     DisposePtr( AlienFile);    AlienFile = 0L;
  757.                 }
  758.                 FSClose( iFileRefI);
  759.             }
  760.         break;
  761.  
  762.         case 'INFO':
  763.             myErr = FSOpen( AlienFileFSSpec->name, 0, &iFileRefI);
  764.             if( myErr == noErr)
  765.             {
  766.                 GetEOF( iFileRefI, &info->fileSize);
  767.             
  768.                 sndSize = 5000L;    // Read only 5000 first bytes for optimisation
  769.                 
  770.                 AlienFile = MADPlugNewPtr( sndSize, init);
  771.                 if( AlienFile == 0L) myErr = MADNeedMemory;
  772.                 else
  773.                 {
  774.                     myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  775.                     if( myErr == noErr)
  776.                     {
  777.                         myErr = ExtractITInfo( info, AlienFile);
  778.                     }
  779.                     DisposePtr( AlienFile);    AlienFile = 0L;
  780.                 }
  781.                 FSClose( iFileRefI);
  782.             }
  783.         break;
  784.         
  785.         default:
  786.             myErr = MADOrderNotImplemented;
  787.         break;
  788.     }
  789.  
  790.     HSetVol( 0L, vRefNum, dirID);
  791.  
  792.     #ifndef powerc
  793.         SetA4( oldA4);
  794.     #endif
  795.     return myErr;
  796. }